package com.eya.rabbitmq.consumer;

import com.eya.rabbitmq.config.RabbitConfigure;
import com.rabbitmq.client.Channel;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * TODO(这个类的作用)
 *
 * @auther: luolin
 * @since: 2018/11/19 11:10
 */
@RabbitListener(queues = RabbitConfigure.SPRING_BOOT_QUEUE_EXCHANGE_B)
@Component
public class TopicMessageConsumer2 {

	int index = 1;

	@RabbitHandler
	public void consume(String content, Channel channel, Message message) throws IOException {
		//告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
		//channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
		System.out.println("TopicMessageConsumer2 get message : " + content);
		if (index >= 2) {
			System.out.println("处理了2次，标记处理成功");
			index = 1;
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} else {
			System.out.println("不处理，标记下次处理");
			channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
			index++;
		}


	}

}
